#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 4096

ssize_t readn(int fd, char *vptr, size_t n)
{
	size_t 		nleft;
	ssize_t 	nread;
	char 		*ptr;

	ptr = vptr;
	nleft = n;
	while (nleft > 0)
	{
		if ( (nread = read(fd, ptr, nleft)) < 0)
		{
			if (errno == EINTR)
				nread = 0;/* call read again */
			else
				return (-1);
		}
		else if (nread == 0)
			break;/* EOF */
		nleft -= nread;
		ptr += nread;
	}
	return (n - nleft);
}

int main()
{
	FILE *fd;
	int n;
	char buf[MAX];

	if ((fd = fopen("test.txt", "r")) < 0) {
		printf("fopen error\n");	
		exit(0);
	}
	if (readn(fileno(fd), buf, MAX) < 0) {
		printf("read error\n");	
		exit(0);
	}	 
	printf("%s\n", buf);	
	printf("%d\n", sizeof(buf));	

	return 0;
}
